package com.hschinese.pronunciation.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.activity.StudyActivity;
import com.hschinese.pronunciation.bean.AppDataBean;

import java.util.List;

public class GridViewAdapter extends BaseAdapter {
	Context mContext;
	List<AppDataBean> mWords;

	public GridViewAdapter(Context context, List<AppDataBean> images) {
		mContext = context;
		mWords = images;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public AppDataBean getItem(int position) {
		return mWords.get(position);
	}

	@Override
	public int getCount() {
		return mWords.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int pos = position;
		View rootView = LayoutInflater.from(mContext).inflate(
				R.layout.word_gridview_item, parent, false);
		TextView textView = (TextView) rootView
				.findViewById(R.id.word_gridview_text);
		Typeface type = Typeface.createFromAsset(mContext.getAssets(), "fonts/hanyupinyin.ttf");
		textView.setTypeface(type);
		textView.setText(getItem(position).getChinese());
		textView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 获取的是当前group的position，转换为大类别的Position
				AppDataBean data = getItem(pos);
				int groupId = data.getGroupID();
				int lessonType = data.getType();
				int position = getPositionInAll(groupId, lessonType, pos);
				System.out.println("xionghy - click position: " + pos + "; position: " + position);
				Intent it = new Intent(mContext, StudyActivity.class);
				it.putExtra("lesson", lessonType);
				it.putExtra("position", position);
				mContext.startActivity(it);
			}

		});
		return rootView;
	}

	private int getPositionInAll(int gid, int type, int pos) {
		if (type == 4) {
			return getPositionInVowel(gid, pos);
		} else {
			return getPositionInConsonant(gid, pos);
		}
	}

	private int getPositionInVowel(int gid, int pos) {
		int position = 0;
		if (gid == 1) {
			position = pos;
		} else if (gid == 2) {
			position = pos + 7;
		} else if(gid == 3) {
			position = pos + 15;
		}
		return position;
	}

	private int getPositionInConsonant(int gid, int pos) {
		int position = 0;
		if (gid == 1) {
			position = pos;
		} else if (gid == 2) {
			position = pos + 4;
		} else if(gid == 3) {
			position = pos + 8;
		} else if (gid == 4) {
			position = pos + 11;
		} else if (gid == 5) {
			position = pos + 14;
		} else if (gid == 6) {
			position = pos + 18;
		}
		return position;
	}
}
